package com.jiangxiaofei.citycloud.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.activity.MainWebActivity;
import com.jiangxiaofei.citycloud.activity.MapAcitivity;
import com.jiangxiaofei.citycloud.activity.park.ParkMapAcitivity;
import com.jiangxiaofei.citycloud.activity.recruit.RecruitWorkNumberActivity;
import com.jiangxiaofei.citycloud.activity.village.VillageAcitivity;
import com.jiangxiaofei.citycloud.activity.WebActivity;
import com.jiangxiaofei.citycloud.bean.HomeMenuItemBean;
import com.jiangxiaofei.citycloud.util.StringUtil;
import com.jiangxiaofei.citycloud.web.SonicJavaScriptInterface;

import java.util.List;

/**
 * Created by JiangXFei on 2017/8/9.
 */

public class MainMenuGridViewAdapter extends BaseAdapter {
    List<HomeMenuItemBean> menuList;
    Context mContext;

    public MainMenuGridViewAdapter(List<HomeMenuItemBean> menuList, Context mContext) {
        this.menuList = menuList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return menuList.size();
    }

    @Override
    public Object getItem(int i) {
        return menuList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_home_main_menus, null, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.tv_menu.setText(menuList.get(i).getName());
        Glide.with(mContext).load(menuList.get(i).getResource())
                .into(viewHolder.img_menu)
                .onLoadStarted(mContext.getResources().getDrawable(R.drawable.ic_csmy));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtil.isHttp(menuList.get(i).getUrl())) {
                    Intent intent = new Intent(mContext, WebActivity.class);
                    intent.putExtra("title", menuList.get(i).getName());
                    mContext.startActivity(intent);
                } else {
                    if (menuList.get(i).getName().equals("城市沙盘")) {
                        Intent intent = new Intent(mContext, MapAcitivity.class);
                        intent.putExtra("title", menuList.get(i).getName());
                        mContext.startActivity(intent);
                    } else if (menuList.get(i).getName().equals("美丽乡村")) {
                        Intent intent = new Intent(mContext, VillageAcitivity.class);
                        mContext.startActivity(intent);
                    } else if (menuList.get(i).getName().equals("城市漫游")) {
                        Intent intent = new Intent(mContext, MainWebActivity.class);
                        intent.putExtra(MainWebActivity.PARAM_URL, "http://720yun.com/t/b41jkpwnzm2?pano_id=3959290");
                        intent.putExtra(SonicJavaScriptInterface.PARAM_CLICK_TIME, System.currentTimeMillis());
                        intent.putExtra(MainWebActivity.PARAM_TITLE, menuList.get(i).getName());
                        mContext.startActivity(intent);
                    } else if (menuList.get(i).getName().equals("城市招工")) {
                        Intent intent = new Intent(mContext, RecruitWorkNumberActivity.class);
                        intent.putExtra("title","城市招工");
                        mContext.startActivity(intent);
                    }else if(menuList.get(i).getName().equals("城市停车")){
                        Intent intent = new Intent(mContext, ParkMapAcitivity.class);
                        intent.putExtra("title","城市停车");
                        mContext.startActivity(intent);
                    }
                }
            }
        });
        return view;
    }

    private class ViewHolder {
        TextView tv_menu;
        ImageView img_menu;

        ViewHolder(View v) {
            tv_menu = v.findViewById(R.id.main_items_tv_menu);
            img_menu = v.findViewById(R.id.main_items_img_bg);
        }
    }
}
